package book;

import java.awt.event.KeyEvent;
import java.sql.CallableStatement;

import java.util.Date;
import java.sql.ResultSet;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author LAPTOP
 */
public class Employee extends javax.swing.JFrame {

    private ConnectDB myConnect;
    private CallableStatement st;
    private ResultSet rs;
    private DefaultTableModel model;
    private int k = 0;
    private int s =0;
    public Employee() {
        initComponents();
        User_emp.setEnabled(false);

        Insert.setEnabled(false);

        try {


            model = (DefaultTableModel) table.getModel();
            myConnect = new ConnectDB();
            myConnect.openConnect();
            String sql = "{call data_Emp}";
            st = myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery();
            if (rs.next()) {
                loadDataToTextFiled(rs);
            }
            LoadData();
            table.setRowSelectionInterval(rs.getRow() - 1, rs.getRow() - 1);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void lockTextFields(boolean status) {
        User_emp.setEnabled(status);

        Insert.setEnabled(status);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        User_emp = new javax.swing.JTextField();
        user_emp = new javax.swing.JLabel();
        pass_emp = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        Name_emp = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        Add_emp = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();
        Insert = new javax.swing.JButton();
        Update = new javax.swing.JButton();
        Delete = new javax.swing.JButton();
        jLabel13 = new javax.swing.JLabel();
        search = new javax.swing.JTextField();
        Find = new javax.swing.JButton();
        Pass_emp = new javax.swing.JTextField();
        Email_emp = new javax.swing.JTextField();
        phone_emp = new javax.swing.JTextField();
        Reset = new javax.swing.JButton();
        chk_u = new javax.swing.JLabel();
        chk_pas = new javax.swing.JLabel();
        chk_name = new javax.swing.JLabel();
        chk_year = new javax.swing.JLabel();
        chk_add = new javax.swing.JLabel();
        chk_email = new javax.swing.JLabel();
        chk_phone = new javax.swing.JLabel();
        date = new com.toedter.calendar.JDateChooser();

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Employee", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tahoma", 1, 24))); // NOI18N

        user_emp.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        user_emp.setForeground(new java.awt.Color(0, 0, 153));
        user_emp.setText("User Employee");

        pass_emp.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        pass_emp.setForeground(new java.awt.Color(0, 0, 153));
        pass_emp.setText("Pass Employee");

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel4.setForeground(new java.awt.Color(0, 0, 153));
        jLabel4.setText("Name");

        jLabel5.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel5.setForeground(new java.awt.Color(0, 0, 153));
        jLabel5.setText("Year of birth");

        jLabel6.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel6.setForeground(new java.awt.Color(0, 0, 153));
        jLabel6.setText("Address");

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(0, 0, 153));
        jLabel7.setText("Email");

        jLabel8.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel8.setForeground(new java.awt.Color(0, 0, 153));
        jLabel8.setText("Phone");

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "User Epmloyee", "Pass Employee", "Name", "Year of birth", "Address", "Email", "Phone"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        table.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tableMouseClicked(evt);
            }
        });
        table.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tableKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(table);

        Insert.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Insert.setForeground(new java.awt.Color(204, 0, 0));
        Insert.setText("Insert");
        Insert.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                InsertActionPerformed(evt);
            }
        });

        Update.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Update.setForeground(new java.awt.Color(204, 0, 0));
        Update.setText("Update");
        Update.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                UpdateActionPerformed(evt);
            }
        });

        Delete.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Delete.setForeground(new java.awt.Color(204, 0, 0));
        Delete.setText("Delete");
        Delete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DeleteActionPerformed(evt);
            }
        });

        jLabel13.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel13.setForeground(new java.awt.Color(0, 0, 153));
        jLabel13.setText("Search user");

        Find.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Find.setText("Find");
        Find.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                FindActionPerformed(evt);
            }
        });

        Reset.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        Reset.setForeground(new java.awt.Color(204, 0, 0));
        Reset.setText("Add new");
        Reset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ResetActionPerformed(evt);
            }
        });

        chk_u.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_u.setForeground(new java.awt.Color(204, 0, 0));

        chk_pas.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_pas.setForeground(new java.awt.Color(204, 0, 0));

        chk_name.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_name.setForeground(new java.awt.Color(204, 0, 0));

        chk_year.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_year.setForeground(new java.awt.Color(204, 0, 0));

        chk_add.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_add.setForeground(new java.awt.Color(204, 0, 0));

        chk_email.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_email.setForeground(new java.awt.Color(204, 0, 0));

        chk_phone.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        chk_phone.setForeground(new java.awt.Color(204, 0, 0));

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel4)
                                    .addComponent(jLabel5))
                                .addGap(59, 59, 59)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(Name_emp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(pass_emp)
                                    .addComponent(user_emp))
                                .addGap(43, 43, 43)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(User_emp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(Pass_emp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(327, 327, 327)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chk_name)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(chk_u)
                                            .addComponent(chk_pas))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel6)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel7))
                                        .addGap(18, 18, 18)
                                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(Add_emp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(phone_emp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(Email_emp, javax.swing.GroupLayout.PREFERRED_SIZE, 245, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(284, 284, 284)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(chk_phone)
                                    .addComponent(chk_email)
                                    .addComponent(chk_add)))
                            .addComponent(chk_year)))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, jPanel1Layout.createSequentialGroup()
                            .addComponent(jLabel13)
                            .addGap(18, 18, 18)
                            .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGap(18, 18, 18)
                            .addComponent(Find)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(Reset)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addComponent(Insert)
                            .addGap(18, 18, 18)
                            .addComponent(Update)
                            .addGap(18, 18, 18)
                            .addComponent(Delete))
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 1230, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel6)
                                .addGap(14, 14, 14)
                                .addComponent(jLabel7))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addComponent(Add_emp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(Email_emp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                    .addComponent(phone_emp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel8))))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chk_name)
                        .addGap(51, 51, 51)
                        .addComponent(chk_year))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(chk_add)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chk_email)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(chk_phone))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(User_emp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(user_emp)
                            .addComponent(chk_u))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(pass_emp)
                            .addComponent(Pass_emp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(chk_pas))
                        .addGap(18, 18, 18)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(Name_emp, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                        .addGap(11, 11, 11)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel5)
                            .addComponent(date, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 263, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(Insert)
                        .addComponent(Update)
                        .addComponent(Delete)
                        .addComponent(Reset))
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel13)
                        .addComponent(search, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(Find)))
                .addContainerGap(24, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 1321, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, 0))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ResetActionPerformed
        lockTextFields(true);
        ClearFiled();

        Insert.setEnabled(true);
    }//GEN-LAST:event_ResetActionPerformed

    private void FindActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_FindActionPerformed
        model.setRowCount(0);
        CallableStatement st;
        ResultSet rs;
        try {

            String sql = "{call search_emp(?)}";
            st = ConnectDB.cn.prepareCall(sql);
            st.setString(1, search.getText());
            rs = st.executeQuery();
            while (rs.next()) {
                model.addRow(new String[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7)});
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            rs = null;
            st = null;
        }
    }//GEN-LAST:event_FindActionPerformed

    private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DeleteActionPerformed
        Delete(st);
    }//GEN-LAST:event_DeleteActionPerformed

    private void UpdateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_UpdateActionPerformed

        JOptionPane.showMessageDialog(this, "Update success");
        Update(st);

    }//GEN-LAST:event_UpdateActionPerformed

    private void InsertActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_InsertActionPerformed
        if (Check() == true) {
            JOptionPane.showMessageDialog(this, "Insert success");
            Add(st);
        }



    }//GEN-LAST:event_InsertActionPerformed

    private void tableKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tableKeyReleased
        int k1 = evt.getKeyCode();
        lockTextFields(false);
        if (k1 == 38) {
            Click(rs);

        } else if (k1 == 40) {
            Click(rs);

        } else if (k1 == KeyEvent.VK_DELETE) {
            Delete(st);
        }
    }//GEN-LAST:event_tableKeyReleased

    private void tableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tableMouseClicked
        Click(rs);
    }//GEN-LAST:event_tableMouseClicked
    public void Click(ResultSet s) {
        User_emp.setEnabled(false);
        try {

            int k = table.getSelectedRow() + 1;
            rs.absolute(k);
            loadDataToTextFiled(rs);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void LoadData() {
        CallableStatement st;
        ResultSet rs;
        try {

            model.setRowCount(0);
            String sql = "{call data_Emp}";
            st = myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rs = st.executeQuery();
            while (rs.next()) {
                model.addRow(new String[]{rs.getString(1), rs.getString(2), rs.getString(3), rs.getString(4), rs.getString(5), rs.getString(6), rs.getString(7)});
            }
            table.setModel(model);
            st.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            st = null;
            rs = null;
        }
    }

    public void loadDataToTextFiled(ResultSet rs) {
        try {
            User_emp.setText(rs.getString(1));
            Pass_emp.setText(rs.getString(2));
            Name_emp.setText(rs.getString(3));
            date.setDate(rs.getDate(4));
            Add_emp.setText(rs.getString(5));
            Email_emp.setText(rs.getString(6));
            phone_emp.setText(rs.getString(7));


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public static String DateToString(Date a) {
        SimpleDateFormat cover = new SimpleDateFormat("yyyy/MM/dd");
        return cover.format(a);
    }

    public void Update(CallableStatement a) {
        CallableStatement st;
        try {
            String sql = "{call Update_Emp(?,?,?,?,?,?,?)}";
            st = myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.setString(1, User_emp.getText());

            st.setString(2, Pass_emp.getText());
            st.setString(3, Name_emp.getText());
            st.setString(4, DateToString(date.getDate()));
            st.setString(5, Add_emp.getText());
            st.setString(6, Email_emp.getText());
            st.setString(7, phone_emp.getText());


            st.execute();
            rs = a.executeQuery();
            LoadData();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void Delete(CallableStatement a) {
        CallableStatement st;
        try {
            String sql = "{call delete_emp(?) }";
            st = myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.setString(1, User_emp.getText());
            st.execute();
            LoadData();
            rs = a.executeQuery();
            if (rs.next()) {
                loadDataToTextFiled(rs);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void ClearFiled() {
        User_emp.setText("");
        Pass_emp.setText("");
        Name_emp.setText("");
        date.setDate(null);
        Add_emp.setText("");

        Email_emp.setText("");
        phone_emp.setText("");
    }

    public void Add(CallableStatement a) {
        CallableStatement st;
        try {

            String sql = "{call insert_emp(?,?,?,?,?,?,?)}";
            st = myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.setString(1, User_emp.getText());
            st.setString(2, Pass_emp.getText());
            st.setString(3, Name_emp.getText());
            st.setString(4, DateToString(date.getDate()));
            st.setString(5, Add_emp.getText());
            st.setString(6, Email_emp.getText());
            st.setString(7, phone_emp.getText());

            st.execute();
            rs = a.executeQuery();
            LoadData();


        } catch (Exception e) {
            e.printStackTrace();
        }


    }
    public boolean check_existUser(String user) {
        CallableStatement st;
        ResultSet rs;
        String sql;
        try {
            
                sql = "{call getEmp(?)}";
            
            st = myConnect.cn.prepareCall(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            st.setString(1, user);
            rs = st.executeQuery();
            if (rs.next()) {
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            st = null;
            rs = null;
        }
        return false;
    }

    public boolean Blank(String s) {
        if (s.equals("")) {
            return false;
        }
        return true;
    }
    public boolean User(String str3) {
        Pattern pt = Pattern.compile("^[A-Za-z1-9]+$");
        Matcher mc = pt.matcher(str3);
        if (!mc.find()) {
            return false;
        }
        return true;
    }

    public boolean Email(String str4) {
        Pattern pt = Pattern.compile(".+@.+\\.[a-zA-Z]+");
        Matcher mc = pt.matcher(str4);
        if (!mc.find()) {
            return false;
        }
        return true;
    }

    

    public boolean phone(String str1) {
        Pattern pt = Pattern.compile("^\\d+$");
        Matcher mc = pt.matcher(str1);
        if (!mc.find()) {
            return false;
        }
        return true;
    }

    public boolean Check() {
        String Email = Email_emp.getText();
        String user = User_emp.getText();
        String pass = Pass_emp.getText();
        String Name = Name_emp.getText();
        String Add = Add_emp.getText();
        String Phone = phone_emp.getText();
        if (!Blank(user)) {
            JOptionPane.showMessageDialog(this, "User can't blank!");
            User_emp.grabFocus();
            return false;
        }else if(!User(user)){
            JOptionPane.showMessageDialog(this, "No space and special character");
            User_emp.grabFocus();
            return false;
               
        }else if (check_existUser(User_emp.getText()) == false) {
            
            JOptionPane.showMessageDialog(this, "Username is exist!");
            User_emp.grabFocus();
            return false;
        }
        
        
        
        if (!Blank(pass)) {
            JOptionPane.showMessageDialog(this, "Pass can't blank!");
            Pass_emp.grabFocus();
            return false;
        }else if(Pass_emp.getText().length()<=3|| Pass_emp.getText().length()>50){
            JOptionPane.showMessageDialog(this, "Pass more than 3 and less 50");
            Pass_emp.grabFocus();
            return false;
        }
        
        if (!Blank(Name)) {
            JOptionPane.showMessageDialog(this, "Name can't blank!");
            Name_emp.grabFocus();
            return false;
        }
        if (!Blank(Add)) {
            JOptionPane.showMessageDialog(this, "Address can't blank!");
            Add_emp.grabFocus();
            return false;
        }
        if (!Blank(Email)) {
            JOptionPane.showMessageDialog(this, "Email can't blank!");
            Email_emp.grabFocus();
            return false;
        }
        
        if (!Blank(Phone)) {
            JOptionPane.showMessageDialog(this, "Phone can't blank!");
            phone_emp.grabFocus();
            return false;
        }else if (!phone(Phone)) {
                JOptionPane.showMessageDialog(this, "Phone just number");
                phone_emp.grabFocus();
                return false;
            } else if (phone_emp.getText().length() < 10 || phone_emp.getText().length() < 15) {
                JOptionPane.showMessageDialog(this, "Phone from 10 to 15 digit!");
                phone_emp.grabFocus();
                return false;
            }

        try {
            
            if (!Email(Email)) {
                JOptionPane.showMessageDialog(this, "Incorrect email!");
                Email_emp.requestFocusInWindow();
                return false;
            } 

        } catch (Exception ex) {
            System.out.println(ex);
        }
        return true;
    }

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {

                new Employee().setVisible(true);

            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField Add_emp;
    private javax.swing.JButton Delete;
    private javax.swing.JTextField Email_emp;
    private javax.swing.JButton Find;
    private javax.swing.JButton Insert;
    private javax.swing.JTextField Name_emp;
    private javax.swing.JTextField Pass_emp;
    private javax.swing.JButton Reset;
    private javax.swing.JButton Update;
    private javax.swing.JTextField User_emp;
    private javax.swing.JLabel chk_add;
    private javax.swing.JLabel chk_email;
    private javax.swing.JLabel chk_name;
    private javax.swing.JLabel chk_pas;
    private javax.swing.JLabel chk_phone;
    private javax.swing.JLabel chk_u;
    private javax.swing.JLabel chk_year;
    private com.toedter.calendar.JDateChooser date;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel pass_emp;
    private javax.swing.JTextField phone_emp;
    private javax.swing.JTextField search;
    private javax.swing.JTable table;
    private javax.swing.JLabel user_emp;
    // End of variables declaration//GEN-END:variables
}
